<?php
// --------------------------------------------------------
// This script is used to converat uwa widget to make it runnable
//
// It will do the following works : 
//
// Please have a look at the code source of a widget and compare it to
// the output of uwa_genereic.php.
//
// http://www.netvibes.com/api/uwa/examples/alexa.html
//
// http://www.netvibes.com/api/uwa/frame/uwa_generic.php?id=123456&moduleUrl=http%3A%2F%2Fwww.netvibes.com%2Fapi%2Fuwa%2Fexamples%2Falexa.html&alexaSite0=netvibes.com&alexaSite1=nytimes.com&commUrl=http://www.yahoo.com.cn/uwa.html
//
// The differences are minimal:
//  - uwa_generic.php set and add the preferences
//  (widget.setPreferences() and widget.data[])
//  - add the id of the widget
//  - add the commUrl if setted and create an iframe to call the commUrl
//   (in the <body/>)
//  - remove cdata tag in <script> or <style>
//  - modify the CSS and JS include URL   
//
// TODO : add security check 
// --------------------------------------------------------
/**
 * @author rainx <jingxu@alibaba-inc.com>
 **/

include "/home/y/share/pear/Yahoo/UWA/uwa_parser.php";

// Analyze the parameters 

$module_url = $_GET['moduleUrl'];
$comm_url   = $_GET['commUrl'];
$id         = $_GET['id'];
if(empty($id)) 
{
    $id = "521";
}

$comm_str = "";
$comm_ifr = "";
if (!empty($comm_url))
{
  $comm_str = "Environment.commUrl = '$comm_url';";
  $comm_ifr = '<iframe style="position:absolute;top:-5px;left:-5px" scrolling="no" frameborder="0" width="0" height="0" id="comm" src="about:blank"></iframe>';
}

$get = $_GET;

// get module 
$content = get($module_url, 30);
if (empty($content))
{
    // contains no content
    die();
}


// get preferences 
$pref = ynuwa_get_preference_from_widget($content);

// remove extra extrenal css and script links
// remove src of style tag
$content = preg_replace("/(<script[^>]*)src=[\"']?[^\"' ]*[\"']?([^>]*>)/i", '${1}${2}', $content );
// remove link tag with href
$content = preg_replace("/(<link[^>]*)href=[\"']?[^\"' ]*[\"']?([^>]*>)/i", '${1}${2}', $content );

// remove cdata tags
$content = str_replace("<![CDATA[", "", $content);
$content = str_replace("]]>", "", $content);

// replace html tag , to remove ns
$content = preg_replace("/(<html[^>]*)xmlns=[\"']?[^\"' ]*[\"']?([^>]*>)/i", '<html xmlns="http://www.w3.org/1999/xhtml">', $content );

// modify body add   <div class="moduleContent" id="moduleContent">
$content = preg_replace("%(<body[^>]*>)(.*)(</body>)%is", '${1} <div class="moduleContent" id="moduleContent"> ${2} </div>  ${3} ', $content);

$content = str_replace("</body>", "$comm_ifr \n </body>", $content);

// construct preferences

$pref_json = json_encode($pref['preferences']);

// set preference data
$data_set = "\n";
foreach($_GET as $k => $v)
{
    if ($k == 'id') continue;
    $v = addslashes($v);
    $data_set .= "widget.data['$k'] = \"$v\"; \n"; 
}

// add scripts and links neccessary
$additonal = <<<HTML
<!-- added by engine -->
<link rel="stylesheet" type="text/css" href="http://cn.yimg.com/uwacache/themes/base/uwa-iframe.css" />
<script type="text/javascript" src="http://cn.yimg.com/uwacache/js/UWA/load.js"></script>
<script type="text/javascript">

var dataFrameLoaded = true;

widget.setPreferences($pref_json);
widget.data['moduleUrl'] = '$module_url';
$data_set; 

widget.id = '$id';

$comm_str

</script>

HTML;

// insert it after head tag
$content = preg_replace("/(<head[^>]*>)/", "\${1}\n$additonal", $content);



header("content-type: text/html; charset=utf-8");
echo $content;



// =================================================
// main end

// Useful functons used in this page:
// =================================================
    /// 获取单个页面的内容和状态 (GET)
    /**
     * @param $url 你需要get页面的url
     * @returns array 返回一个数组，第一个元素是返回的header状态，第二个是抓取网页正文的内容
     */
    function get($url, $timeout=null)
    {
        if ($timeout == null)
        {
            $timeout = 5 ;
        }

        $ch = curl_init();
        if(!$ch) {
            return null;
        }
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
        $data = curl_exec($ch);
        $status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $data;
    }
?>
